package org.finmanager.core;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by Kelvin on 20/3/2014.
 * <p/>
 * This class contains a tab label and a close button "x" to close the tab
 */
public class TabComponent extends JPanel {
	private final JTabbedPane pane;

	public TabComponent(final JTabbedPane pane) {
		super(new FlowLayout(FlowLayout.LEFT, 0, 0));
		if (pane == null) {
			throw new NullPointerException("TabbedPane is null");
		}
		this.pane = pane;
		setOpaque(false);

		JLabel label = new JLabel() {
			public String getText() {
				int i = pane.indexOfTabComponent(TabComponent.this);
				if (i != -1) {
					return pane.getTitleAt(i);
				}
				return null;
			}
		};

		add(label);
		//add more space between the label and the button
		label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
		//tab button (close button)
		JButton button = new TabButton();
		add(button);
		//add more space to the top of the component
		setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
	}

	// The close button
	// Currently the close button is represented by an "x" in a button
	// TODO: override paintComponent method to paint an "x"
	private class TabButton extends JButton implements ActionListener {
		public TabButton() {
			int size = 17;
			setPreferredSize(new Dimension(size, size));
			//Make the button looks the same for all Laf's
			setUI(new BasicButtonUI());
			//Make it transparent
			setContentAreaFilled(false);
			//No need to be focusable
			setFocusable(false);
			setBorder(BorderFactory.createEtchedBorder());
			setBorderPainted(false);
			//Making nice rollover effect
			//we use the same listener for all buttons
			addMouseListener(buttonMouseListener);
			setRolloverEnabled(true);
			//Close the proper tab by clicking the button
			addActionListener(this);
		}

		//we don't want to update UI for this button
		public void updateUI() {
		}

		//paint the cross
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D) g.create();
			//shift the image for pressed buttons
			g2.setStroke(new BasicStroke(2));
			g2.setColor(Color.DARK_GRAY);
			if (getModel().isRollover()) {
				g2.setColor(Color.LIGHT_GRAY);
			}
			int delta = 6;
			int width = 5;
			g2.drawLine(delta, delta, delta + width, delta + width);
			g2.drawLine(delta, delta + width, delta + width, delta);

			//g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
			//g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
			g2.dispose();
		}

		public void actionPerformed(ActionEvent e) {
			int i = pane.indexOfTabComponent(TabComponent.this);
			if (i != -1) {
				pane.remove(i);
			}
		}
	}

	private final static MouseListener buttonMouseListener = new MouseAdapter() {
		public void mouseEntered(MouseEvent e) {
			Component component = e.getComponent();
			if (component instanceof AbstractButton) {
				AbstractButton button = (AbstractButton) component;
				button.setBorderPainted(false);
			}
		}

		public void mouseExited(MouseEvent e) {
			Component component = e.getComponent();
			if (component instanceof AbstractButton) {
				AbstractButton button = (AbstractButton) component;
				button.setBorderPainted(false);
			}
		}
	};
}
